Recurrences
Tags: #◇Computer Science #◇CCDSALG #◇Recurrences #◇Problem Solving #◇M1
◇Overview
A recurrence is an equality or inequality that describes a function over integers or reals using the function itself. It is primarily used to describe the running time of recursive algorithms. A typical recurrence contains more than one case depending on the argument:
- Base case: The terminating condition of the recursion.
- Recursive case: The part of the function that calls itself with smaller arguments.
By convention, if a recurrence is stated without an explicit base case, it is assumed to be algorithmic. This means that for every sufficiently large threshold constant :
- For all , .
- For all , every path of recursion terminates in a defined base case within a finite number of recursive invocations.
◇Important Formulas
Solving Recurrences: Iteration Method
The iteration method involves expanding the recurrence repeatedly to identify a pattern. By expressing as a summation of terms, we can simplify it into a closed form or explicit formula and determine its Big-Oh complexity.
Useful Summation Formulas
- Constant:
- Arithmetic Series:
- Sum of Squares:
- Sum of Cubes:
- Geometric Series:
Useful Logarithm Formulas
For all real and logarithm bases :
- Change of base:
- Logarithmic identity:
- Product rule:
- Reciprocal rule:
- Base flip:
◇Worked Examples
Example 1: Formulating a Recurrence for Factorial
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
The recurrence relation for this algorithm is:
where is the constant time taken for the base case, and is the constant time taken for the multiplication and function overhead in the recursive step.
Example 2: Iteration Method
Determine the explicit formula and Big-Oh notation for:
We expand :
Generalizing to iterations:
To reach the base case where , we set . Substituting this back:
Therefore, the closed form is , which is .
◇References & Navigation
- Related Notes: ◇Recurrences